
############################################
# GHS 
# Date: MAY 19 2014
###################################################################

#################DATA PREPARATION ################################

D=read.dta("data/GHS_Replication_NEWBLOCKS.dta", convert.factors=FALSE) # after converting the excel into Stata format.

# New variables
#D$BLOCK=matrix(NA, length(D$V1_NAME))
#	.b =sort(unique(D$V1_NAME))
#	for(i in 1:length(.b)){D$BLOCK[D$V1_NAME==.b[i]]<-i}   

D$ENG   <-  (rank(D$ENGAGED)>max(rank(D$ENGAGED))*(1-mean(D$SMS[which(D$V6_SMS==1,)])))



## create rank vars

D$WEALTH_RANK  = 100-100*rank(D$POOR)/7553
D$DIST_RANK    = 100*rank(D$distance_capital)/7553

D$ENGAGED_RANK = 100*rank(D$ENGAGED)/7553
D$ACCESS_RANK  = 100*rank(D$ACCESS)/7553
D$MARG_RANK    = 100*rank(D$MARG)/7553

D$MIDDLE <- ifelse(D$WEALTH_RANK>=25 & D$WEALTH_RANK<=75, 1, 0)


# SUBSET DATA USED FOR ANALYSIS ON EXPERIMENTAL POPULATION ONLY
DS = D[D$V6_SMS==1,]

.pctile <-  1- mean(DS$SMS)
DS$ENG  <- (rank(DS$ENGAGED)>max(rank(DS$ENGAGED))*.pctile)    

###################################################################

###################PERMUTATIONS####################################

# Create permutations matrix based on original randomization procedure
# Within block sampling procedure
# sample43
price.sample = function(.blocksize) ifelse(.blocksize<3, 
                                           return(sample(0:2, .blocksize)), 
                                           return(sample(c(0:2, sample(0:2,.blocksize-3))))
)


DS$block.size = sapply(DS$DICT_PCODE2011, function(i) length(DS$DICT_PCODE2011[DS$DICT_PCODE2011==i]))

# Redo Permutations

sims = 5000                       #set to 5000 for paper reuslts
PM =  matrix(NA, nrow(DS), sims)
for(j in 1:sims){
  for(k in unique(DS$DICT_PCODE2011)) {
    PM[DS$DICT_PCODE2011==k, j]<-price.sample(mean(DS$block.size[DS$DICT_PCODE2011==k])) 
  }
}

save(PM, file = "PM_NEWBLOCKS.RData")





